﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace RobotGameData
{
    public class MapReader
    {
       public Map  LoadMap(String strPath)
       {
           Map map = new Map();
           XmlDocument document = new XmlDocument();
           document.Load(strPath);

           XmlElement root = document.DocumentElement;
           string[] param = new string[] { " ", "\r\n","\t" };
           XmlNode textureName = document.SelectNodes("//TextureName")[0];
           string[] name = (textureName.InnerXml.Trim()).Split(param, StringSplitOptions.RemoveEmptyEntries);
           map.TextureName = name[0].Trim();
           XmlNode mapDimensions = document.SelectNodes("//MapDimensions")[0];
           string[] dimension = (mapDimensions.InnerXml.Trim()).Split(param, StringSplitOptions.RemoveEmptyEntries);
           int dX = int.Parse(dimension[0].Trim());
           int dY = int.Parse(dimension[1].Trim());
           map.MapDimensions = new Point(dX,dY);
           XmlNode tileSize = document.SelectNodes("//TileSize")[0];
           string[] tile = (tileSize.InnerXml.Trim()).Split(param, StringSplitOptions.RemoveEmptyEntries);
           int tX = int.Parse(tile[0].Trim());
           int tY = int.Parse(tile[1].Trim());
           map.TileSize = new Point(tX,tY);

           XmlNode baseLayer = document.SelectNodes("//BaseLayer")[0];
           
           string[] bLayer = (baseLayer.InnerXml.Trim()).Split(param, StringSplitOptions.RemoveEmptyEntries);
           map.BaseLayer = new int[bLayer.Length];
           for (int i = 0; i < bLayer.Length; i++)
           {
               map.BaseLayer[i] = int.Parse(bLayer[i].Trim());
           }

           XmlNode fringeLayer = document.SelectNodes("//FringeLayer")[0];

           string[] fLayer = (fringeLayer.InnerXml.Trim()).Split(param, StringSplitOptions.RemoveEmptyEntries);
           map.FringeLayer = new int[fLayer.Length];
           for (int i = 0; i < fLayer.Length; i++)
           {
               map.FringeLayer[i] = int.Parse(fLayer[i].Trim());
           }

           XmlNode objectLayer = document.SelectNodes("//ObjectLayer")[0];

           string[] oLayer = (objectLayer.InnerXml.Trim()).Split(param, StringSplitOptions.RemoveEmptyEntries);
           map.ObjectLayer = new int[oLayer.Length];
           for (int i = 0; i < oLayer.Length; i++)
           {
               map.ObjectLayer[i] = int.Parse(oLayer[i].Trim());
           }

           XmlNode collisionLayer = document.SelectNodes("//CollisionLayer")[0];

           string[] cLayer = (collisionLayer.InnerXml.Trim()).Split(param, StringSplitOptions.RemoveEmptyEntries);
           map.CollisionLayer = new int[cLayer.Length];
           for (int i = 0; i < cLayer.Length; i++)
           {
               map.CollisionLayer[i] = int.Parse(cLayer[i].Trim());
           }

           map.Portals = new List<Portal>();
           XmlNodeList portals = document.SelectNodes("//Portals");
           foreach (XmlNode node in portals)
           {
               Portal portal = new Portal();
               XmlNode position = node.SelectNodes("//Position")[0];
               string[] positions = (position.InnerXml.Trim()).Split(param, StringSplitOptions.RemoveEmptyEntries);
               portal.Position = new Point(int.Parse(positions[0].Trim()),int.Parse(positions[1].Trim()));
               XmlNode destinationMap = node.SelectNodes("//DestinationMap")[0];
               portal.DestinationMap = destinationMap.InnerXml.Trim();
               XmlNode destinationTileLocation = node.SelectNodes("//DestinationTileLocation")[0];
               string[] destinations = (destinationTileLocation.InnerXml.Trim()).Split(param, StringSplitOptions.RemoveEmptyEntries);
               portal.DestinationTileLocation = new Point(int.Parse(destinations[0].Trim()), int.Parse(destinations[1].Trim()));
               map.Portals.Add(portal);
           }

           return map;

       }
    }
}
